"""
日历组件请求和响应模型

包含所有与日历和日程相关的数据模型：
- Calendar: 日历相关的请求/响应模型
- Event: 日程相关的请求/响应模型
- EventRecurrence: 重复规则相关的请求/响应模型
- EventReminder: 提醒设置相关的请求/响应模型
- EventParticipant: 参与者相关的请求/响应模型
- CalendarShare: 日历分享相关的请求/响应模型
"""

from pydantic import BaseModel, Field, EmailStr
from typing import Optional, List, Dict, Any
from datetime import datetime
from enum import Enum
from ..models import (
    CalendarVisibility, EventStatus, EventPriority, RepeatType,
    ReminderType, SharePermission, ParticipantStatus
)


class CalendarBase(BaseModel):
    """日历基础模型"""
    name: str = Field(..., min_length=1, max_length=100, description="日历名称")
    description: Optional[str] = Field(None, description="日历描述")
    color: str = Field("#007bff", description="日历颜色，十六进制格式")
    timezone: str = Field("UTC", description="时区")
    visibility: CalendarVisibility = Field(CalendarVisibility.PRIVATE, description="可见性")
    default_permission: SharePermission = Field(SharePermission.VIEW, description="默认权限")


class CalendarCreate(CalendarBase):
    """创建日历请求模型"""
    is_default: bool = Field(False, description="是否为默认日历")


class CalendarUpdate(BaseModel):
    """更新日历请求模型"""
    name: Optional[str] = Field(None, min_length=1, max_length=100, description="日历名称")
    description: Optional[str] = Field(None, description="日历描述")
    color: Optional[str] = Field(None, description="日历颜色")
    timezone: Optional[str] = Field(None, description="时区")
    visibility: Optional[CalendarVisibility] = Field(None, description="可见性")
    default_permission: Optional[SharePermission] = Field(None, description="默认权限")
    is_default: Optional[bool] = Field(None, description="是否为默认日历")


class CalendarResponse(CalendarBase):
    """日历响应模型"""
    id: int = Field(..., description="日历ID")
    owner_id: int = Field(..., description="所有者ID")
    is_default: bool = Field(..., description="是否为默认日历")
    create_time: datetime = Field(..., description="创建时间")
    update_time: datetime = Field(..., description="更新时间")
    
    class Config:
        from_attributes = True


class EventBase(BaseModel):
    """日程基础模型"""
    title: str = Field(..., min_length=1, max_length=200, description="日程标题")
    description: Optional[str] = Field(None, description="日程描述")
    location: Optional[str] = Field(None, max_length=200, description="地点")
    url: Optional[str] = Field(None, max_length=500, description="相关链接")
    start_time: datetime = Field(..., description="开始时间")
    end_time: datetime = Field(..., description="结束时间")
    all_day: bool = Field(False, description="是否为全天事件")
    timezone: str = Field("UTC", description="时区")
    status: EventStatus = Field(EventStatus.CONFIRMED, description="状态")
    priority: EventPriority = Field(EventPriority.MEDIUM, description="优先级")
    visibility: CalendarVisibility = Field(CalendarVisibility.PRIVATE, description="可见性")
    busy: bool = Field(True, description="是否显示为忙碌")
    transparent: bool = Field(False, description="是否透明")
    extended_properties: Optional[Dict[str, Any]] = Field(None, description="扩展属性")


class EventCreate(EventBase):
    """创建日程请求模型"""
    calendar_id: int = Field(..., description="日历ID")
    tag_ids: Optional[List[int]] = Field(None, description="标签ID列表")
    participant_emails: Optional[List[str]] = Field(None, description="参与者邮箱列表")
    reminders: Optional[List[Dict[str, Any]]] = Field(None, description="提醒设置")


class EventUpdate(EventBase):
    """更新日程请求模型"""
    title: Optional[str] = Field(None, min_length=1, max_length=200, description="日程标题")
    description: Optional[str] = Field(None, description="日程描述")
    location: Optional[str] = Field(None, max_length=200, description="地点")
    url: Optional[str] = Field(None, max_length=500, description="相关链接")
    start_time: Optional[datetime] = Field(None, description="开始时间")
    end_time: Optional[datetime] = Field(None, description="结束时间")
    all_day: Optional[bool] = Field(None, description="是否为全天事件")
    timezone: Optional[str] = Field(None, description="时区")
    status: Optional[EventStatus] = Field(None, description="状态")
    priority: Optional[EventPriority] = Field(None, description="优先级")
    visibility: Optional[CalendarVisibility] = Field(None, description="可见性")
    busy: Optional[bool] = Field(None, description="是否显示为忙碌")
    transparent: Optional[bool] = Field(None, description="是否透明")
    extended_properties: Optional[Dict[str, Any]] = Field(None, description="扩展属性")
    tag_ids: Optional[List[int]] = Field(None, description="标签ID列表")


class EventResponse(EventBase):
    """日程响应模型"""
    id: int = Field(..., description="日程ID")
    calendar_id: int = Field(..., description="日历ID")
    creator_id: int = Field(..., description="创建者ID")
    parent_event_id: Optional[int] = Field(None, description="父日程ID")
    create_time: datetime = Field(..., description="创建时间")
    update_time: datetime = Field(..., description="更新时间")
    
    # 关联数据
    calendar: Optional[CalendarResponse] = Field(None, description="日历信息")
    tags: Optional[List[Dict[str, Any]]] = Field(None, description="标签列表")
    participants: Optional[List[Dict[str, Any]]] = Field(None, description="参与者列表")
    reminders: Optional[List[Dict[str, Any]]] = Field(None, description="提醒设置")
    recurrence: Optional[Dict[str, Any]] = Field(None, description="重复规则")
    
    class Config:
        from_attributes = True


class EventRecurrenceBase(BaseModel):
    """重复规则基础模型"""
    repeat_type: RepeatType = Field(..., description="重复类型")
    interval: int = Field(1, ge=1, description="重复间隔")
    repeat_days: Optional[List[int]] = Field(None, description="重复星期几（0-6，0为周日）")
    repeat_month_day: Optional[int] = Field(None, ge=1, le=31, description="重复日期")
    repeat_month: Optional[int] = Field(None, ge=1, le=12, description="重复月份")
    repeat_week: Optional[int] = Field(None, ge=1, le=5, description="重复第几周")
    repeat_week_day: Optional[int] = Field(None, ge=0, le=6, description="重复星期几")
    end_date: Optional[datetime] = Field(None, description="结束日期")
    end_count: Optional[int] = Field(None, ge=1, description="重复次数")
    exception_dates: Optional[List[datetime]] = Field(None, description="例外日期列表")


class EventRecurrenceCreate(EventRecurrenceBase):
    """创建重复规则请求模型"""
    pass


class EventRecurrenceUpdate(EventRecurrenceBase):
    """更新重复规则请求模型"""
    repeat_type: Optional[RepeatType] = Field(None, description="重复类型")
    interval: Optional[int] = Field(None, ge=1, description="重复间隔")


class EventRecurrenceResponse(EventRecurrenceBase):
    """重复规则响应模型"""
    id: int = Field(..., description="重复规则ID")
    event_id: int = Field(..., description="日程ID")
    create_time: datetime = Field(..., description="创建时间")
    update_time: datetime = Field(..., description="更新时间")
    
    class Config:
        from_attributes = True


class EventReminderBase(BaseModel):
    """提醒设置基础模型"""
    reminder_type: ReminderType = Field(..., description="提醒类型")
    minutes_before: int = Field(..., ge=0, description="提前多少分钟提醒")
    message: Optional[str] = Field(None, description="提醒消息")


class EventReminderCreate(EventReminderBase):
    """创建提醒设置请求模型"""
    pass


class EventReminderUpdate(EventReminderBase):
    """更新提醒设置请求模型"""
    reminder_type: Optional[ReminderType] = Field(None, description="提醒类型")
    minutes_before: Optional[int] = Field(None, ge=0, description="提前多少分钟提醒")


class EventReminderResponse(EventReminderBase):
    """提醒设置响应模型"""
    id: int = Field(..., description="提醒设置ID")
    event_id: int = Field(..., description="日程ID")
    is_sent: bool = Field(..., description="是否已发送")
    sent_at: Optional[datetime] = Field(None, description="发送时间")
    is_confirmed: bool = Field(..., description="是否已确认")
    confirmed_at: Optional[datetime] = Field(None, description="确认时间")
    create_time: datetime = Field(..., description="创建时间")
    update_time: datetime = Field(..., description="更新时间")
    
    class Config:
        from_attributes = True


class EventParticipantBase(BaseModel):
    """参与者基础模型"""
    email: Optional[EmailStr] = Field(None, description="邮箱地址")
    name: Optional[str] = Field(None, max_length=100, description="姓名")
    status: ParticipantStatus = Field(ParticipantStatus.NEEDS_ACTION, description="参与状态")
    permission: SharePermission = Field(SharePermission.VIEW, description="权限")
    response_message: Optional[str] = Field(None, description="响应消息")
    notify_email: bool = Field(True, description="邮件通知")
    notify_popup: bool = Field(True, description="弹窗通知")


class EventParticipantCreate(EventParticipantBase):
    """创建参与者请求模型"""
    user_id: Optional[int] = Field(None, description="用户ID")


class EventParticipantUpdate(EventParticipantBase):
    """更新参与者请求模型"""
    status: Optional[ParticipantStatus] = Field(None, description="参与状态")
    permission: Optional[SharePermission] = Field(None, description="权限")
    response_message: Optional[str] = Field(None, description="响应消息")
    notify_email: Optional[bool] = Field(None, description="邮件通知")
    notify_popup: Optional[bool] = Field(None, description="弹窗通知")


class EventParticipantResponse(EventParticipantBase):
    """参与者响应模型"""
    id: int = Field(..., description="参与者ID")
    event_id: int = Field(..., description="日程ID")
    user_id: Optional[int] = Field(None, description="用户ID")
    responded_at: Optional[datetime] = Field(None, description="响应时间")
    create_time: datetime = Field(..., description="创建时间")
    update_time: datetime = Field(..., description="更新时间")
    
    class Config:
        from_attributes = True


class CalendarShareBase(BaseModel):
    """日历分享基础模型"""
    shared_with_email: Optional[EmailStr] = Field(None, description="分享给邮箱")
    permission: SharePermission = Field(SharePermission.VIEW, description="分享权限")


class CalendarShareCreate(CalendarShareBase):
    """创建日历分享请求模型"""
    shared_with_id: Optional[int] = Field(None, description="分享给用户ID")


class CalendarShareUpdate(CalendarShareBase):
    """更新日历分享请求模型"""
    permission: Optional[SharePermission] = Field(None, description="分享权限")


class CalendarShareResponse(CalendarShareBase):
    """日历分享响应模型"""
    id: int = Field(..., description="分享ID")
    calendar_id: int = Field(..., description="日历ID")
    shared_with_id: Optional[int] = Field(None, description="分享给用户ID")
    is_accepted: bool = Field(..., description="是否已接受")
    accepted_at: Optional[datetime] = Field(None, description="接受时间")
    create_time: datetime = Field(..., description="创建时间")
    update_time: datetime = Field(..., description="更新时间")
    
    class Config:
        from_attributes = True


class EventTagBase(BaseModel):
    """标签基础模型"""
    name: str = Field(..., min_length=1, max_length=50, description="标签名称")
    color: str = Field("#6c757d", description="标签颜色")
    description: Optional[str] = Field(None, description="标签描述")


class EventTagCreate(EventTagBase):
    """创建标签请求模型"""
    pass


class EventTagUpdate(EventTagBase):
    """更新标签请求模型"""
    name: Optional[str] = Field(None, min_length=1, max_length=50, description="标签名称")
    color: Optional[str] = Field(None, description="标签颜色")
    description: Optional[str] = Field(None, description="标签描述")


class EventTagResponse(EventTagBase):
    """标签响应模型"""
    id: int = Field(..., description="标签ID")
    user_id: int = Field(..., description="创建者ID")
    usage_count: int = Field(..., description="使用次数")
    create_time: datetime = Field(..., description="创建时间")
    update_time: datetime = Field(..., description="更新时间")
    
    class Config:
        from_attributes = True


class EventCommentBase(BaseModel):
    """日程评论基础模型"""
    content: str = Field(..., min_length=1, description="评论内容")
    parent_comment_id: Optional[int] = Field(None, description="父评论ID")
    attachments: Optional[List[Dict[str, Any]]] = Field(None, description="附件信息")


class EventCommentCreate(EventCommentBase):
    """创建日程评论请求模型"""
    pass


class EventCommentUpdate(EventCommentBase):
    """更新日程评论请求模型"""
    content: Optional[str] = Field(None, min_length=1, description="评论内容")
    attachments: Optional[List[Dict[str, Any]]] = Field(None, description="附件信息")


class EventCommentResponse(EventCommentBase):
    """日程评论响应模型"""
    id: int = Field(..., description="评论ID")
    event_id: int = Field(..., description="日程ID")
    user_id: int = Field(..., description="评论者ID")
    create_time: datetime = Field(..., description="创建时间")
    update_time: datetime = Field(..., description="更新时间")
    
    class Config:
        from_attributes = True


# 搜索和过滤模型
class EventSearchRequest(BaseModel):
    """日程搜索请求模型"""
    keyword: Optional[str] = Field(None, description="搜索关键词")
    start_date: Optional[datetime] = Field(None, description="开始日期")
    end_date: Optional[datetime] = Field(None, description="结束日期")
    calendar_ids: Optional[List[int]] = Field(None, description="日历ID列表")
    tag_ids: Optional[List[int]] = Field(None, description="标签ID列表")
    status: Optional[EventStatus] = Field(None, description="状态")
    priority: Optional[EventPriority] = Field(None, description="优先级")
    creator_id: Optional[int] = Field(None, description="创建者ID")
    participant_id: Optional[int] = Field(None, description="参与者ID")
    page: int = Field(1, ge=1, description="页码")
    page_size: int = Field(20, ge=1, le=100, description="每页大小")


class EventSearchResponse(BaseModel):
    """日程搜索响应模型"""
    events: List[EventResponse] = Field(..., description="日程列表")
    total: int = Field(..., description="总数")
    page: int = Field(..., description="当前页码")
    page_size: int = Field(..., description="每页大小")
    total_pages: int = Field(..., description="总页数")
    
    class Config:
        from_attributes = True